package com.bingo.cf.certification.server.api.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bingo.cf.certification.server.api.entity.DemoTable;
import com.bingo.cf.certification.server.api.service.IDemoTableService;
import com.tip.restful.core.common.helper.JSONHelper;
import com.tip.restful.core.common.helper.RedisUtils;
import com.tip.restful.core.common.helper.StringHelper;
import com.tip.restful.core.controller.ControllerBase;
import com.tip.restful.core.controller.helper.JsonResultHelper;
import com.tip.restful.core.controller.result.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Auto-Generated
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/api/demoTable")
public class DemoTableController extends ControllerBase<DemoTable, IDemoTableService> {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    // region 查询分页

    /**
     * 查询分页Demo1
     *
     * @param state 状态
     * @return
     */
    @ApiOperation("sql查询分页例子1")
    @GetMapping("selectDemoPage1")
    public JsonResult<?> selectDemoPage1(int state) {
        // 分页参数
        // 当前页
        int current = 1;
        // 每页大小
        int size = 5;
        // 初始化page参数（分页必须传入page对象）
        Page<HashMap<String, Object>> page = new Page(current, size);
        // 执行服务方法
        page = baseService.selectDemoPage1(page, state);
        return JsonResultHelper.ResultToJson(page);
    }

    /**
     * 查询分页Demo2
     *
     * @param state
     * @return
     */
    @ApiOperation("sql查询分页例子2")
    @GetMapping("selectDemoPage2")
    public JsonResult<?> selectDemoPage2(int state) { // 分页参数
        // 当前页
        int current = 1;
        // 每页大小
        int size = 5;
        // 初始化page参数（分页必须传入page对象）
        Page<HashMap<String, Object>> page = new Page(current, size);
        EntityWrapper<HashMap<String, Object>> ew = new EntityWrapper<>();
        ew.where(" STATE = {0} ", state);
        // 执行服务方法
        page = baseService.selectDemoPage2(page, ew);
        return JsonResultHelper.ResultToJson(page);
    }

    /**
     * 查询分页Demo1
     *
     * @param state
     * @return
     */
    @ApiOperation("sql查询分页例子3")
    @GetMapping("selectDemoPage3")
    public JsonResult<?> selectDemoPage3(int state) { // 分页参数
        // 当前页
        int current = 1;
        // 每页大小
        int size = 5;
        // 初始化page参数（分页必须传入page对象）
        Page<DemoTable> page = new Page(current, size);
        EntityWrapper<DemoTable> ew = new EntityWrapper<>();
        ew.where(" STATE = {0} ", state);
        // 执行服务方法
        page = baseService.selectDemoPage3(page, ew);
        return JsonResultHelper.ResultToJson(page);
    }
    // endregion

    // region redis 缓存操作DEMO
    /**
     * 缓存工具类，也可直接实例化RedisTemplate对象进行缓存操作;
     */
    @Autowired
    RedisUtils redisUtils;
//
//    @Autowired
//    private RedisTemplate redisTemplate;


    /**
     * 缓存DEMO
     *
     * @return
     */
    @GetMapping("cacheDemo")
    public JsonResult<?> cacheDemo() {
//        StringBuilder sb = new StringBuilder();
        Map<String, String> map = new HashMap<>();
        DemoTable demoTable = new DemoTable();
        String key = StringHelper.newUniqueId();
        demoTable.setDemoId(key);
        demoTable.setDemoName(key);
        boolean isSuccess = redisUtils.set(key, demoTable);
        String msg = String.format(" redisUtils.set result:%s ", isSuccess);
        map.put("1_redisUtils_set", msg);
        logger.debug(msg);
        Object value = redisUtils.get(key);
        DemoTable demoTable2 = JSONHelper.jsonToEntity(JSONHelper.toJson(value), DemoTable.class);
        msg = String.format(" redisUtils.get demoTable result:%s  ", JSONHelper.toJson(demoTable2));
        map.put("2_redisUtils_get", msg);
        logger.debug(msg);
        redisUtils.del(key);
        msg = String.format("redisUtils.del key:%s", key);
        map.put("3_redisUtils_del", msg);
        logger.debug(msg);
        boolean hasKey = redisUtils.hasKey(key);
        msg = String.format(" redisUtils.hasKey key:%s result:%s ", key, hasKey);
        map.put("4_redisUtils_hasKey", msg);
        logger.debug(msg);
        return JsonResultHelper.ResultToJson(map);
    }

    // endregion
}
